/**
 * 资源受权管理初始化
 */
var ResourceAuth = {
	resourceId:null,
    backendResourceTree: null,
	frontResourceTree: null,
	apiResourceTree: null,
    roleTree:null,
    orgTree:null,
    layerIndex: -1,
    parentId:0
};

ResourceAuth.onClickResource = function (e, treeId, treeNode) {
	if(treeNode.id==0){
		Sys.info("根节点没有必要设置权限哦！")
		return;
	}
	ResourceAuth.orgTree.getObject().checkAllNodes(false);
	ResourceAuth.roleTree.getObject().checkAllNodes(false);
    ResourceAuth.resourceId = treeNode.id;
    //load权限
    Sys.ajax(Sys.backendPath + "/permission/loadPermsByResourceId",{resourceId:treeNode.id}, function (res) {
        var roleIds = res.data.role;
        if(roleIds){
        	for (var i = 0; i < roleIds.length; i++) {
        	    var node = ResourceAuth.roleTree.getObject().getNodeByParam("id", roleIds[i], null);
        	    if(node){
        	    	ResourceAuth.roleTree.getObject().checkNode(node, true, false);
        	    }
			}
        }
        var deptIds = res.data.dept;
        if(deptIds){
        	for (var i = 0; i < deptIds.length; i++) {
        	    var node = ResourceAuth.orgTree.getObject().getNodeByParam("id", deptIds[i], null);
        	    if(node){
        	    	ResourceAuth.orgTree.getObject().checkNode(node, true, false);
        	    }
			}
        }
        var userIds = res.data.user;
        if(userIds){
        	for (var i = 0; i < userIds.length; i++) {
        	    var node = ResourceAuth.orgTree.getObject().getNodeByParam("id", userIds[i], null);
        	    if(node){
        	    	ResourceAuth.orgTree.getObject().checkNode(node, true, false);
        	    }
			}
        }
    });
};

ResourceAuth.roleTreeOnCheck = function (event, treeId, treeNode) {
	if((!(ResourceAuth.resourceId))||ResourceAuth.resourceId==0){
		Sys.info("请先选择要设置的资源！")
		return;
	}
	var _data = {
		resourceId:ResourceAuth.resourceId,
		principalId:treeNode.id,
		principalType:1,
		isAdd:treeNode.checked
	};
    //设置权限
    Sys.ajax(Sys.backendPath + "/permission/auth",_data, function (data) {
        Sys.info("权限设置成功!*^_^*");
    });
};

ResourceAuth.orgTreeOnCheck = function (event, treeId, treeNode) {
	if((!(ResourceAuth.resourceId))||ResourceAuth.resourceId==0){
		Sys.info("请先选择要设置的资源！")
		return;
	}
	var _data = {
		resourceId:ResourceAuth.resourceId,
		principalId:treeNode.id,
		principalType:treeNode.isParent?2:3,
		isAdd:treeNode.checked
	};
    //设置权限
    Sys.ajax(Sys.backendPath + "/permission/auth",_data, function (data) {
        Sys.info("权限设置成功!*^_^*");
    });
};

$(function () {
    var backendResourceTree = new $ZTree("backendResourceTree", Sys.backendPath + "/tree/resource/backend");
    backendResourceTree.bindOnClick(ResourceAuth.onClickResource);
    backendResourceTree.init();
    ResourceAuth.backendResourceTree = backendResourceTree;
    fuzzySearch('backendResourceTree','#backendSeachKey',null,true);
    
    var frontResourceTree = new $ZTree("frontResourceTree", Sys.backendPath + "/tree/resource/front");
    frontResourceTree.bindOnClick(ResourceAuth.onClickResource);
    frontResourceTree.init();
    ResourceAuth.frontResourceTree = frontResourceTree;
    fuzzySearch('frontResourceTree','#frontSeachKey',null,true);
    
    var apiResourceTree = new $ZTree("apiResourceTree", Sys.backendPath + "/tree/resource/api");
    apiResourceTree.bindOnClick(ResourceAuth.onClickResource);
    apiResourceTree.init();
    ResourceAuth.apiResourceTree = apiResourceTree;
    fuzzySearch('apiResourceTree','#apiSeachKey',null,true);

    var roleTreeSetting = {
        check: {
            enable: true,
            chkboxType: {
                "Y": "",
                "N": ""
            }
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
    		onCheck: ResourceAuth.roleTreeOnCheck
    	}
    };
    var roleTree = new $ZTree("roleTree", Sys.backendPath + "/tree/role/everyone");
    roleTree.setSettings(roleTreeSetting);
    roleTree.init();
    ResourceAuth.roleTree = roleTree
    var roleRoot = roleTree.getObject().getNodeByParam("id", 0, null);
    roleRoot.nocheck = true;
    roleTree.getObject().updateNode(roleRoot);

    var orgTreeSetting = {
        check: {
            enable: true,
            chkboxType: {
                "Y": "",
                "N": ""
            }
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
    		onCheck: ResourceAuth.orgTreeOnCheck
    	}
    };
    var orgTree = new $ZTree("orgTree", Sys.backendPath + "/tree/user/admin");
    orgTree.setSettings(orgTreeSetting);
    orgTree.init();
    ResourceAuth.orgTree = orgTree
    var orgRoot = orgTree.getObject().getNodeByParam("id", 0, null);
    orgRoot.nocheck = true;
    orgTree.getObject().updateNode(orgRoot);
    
});
